package com.dragon.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * @Description ：贷款时间工具类及其他工具类
 * @Author ：qiaohonglei
 * @Date ：2017年02月08日19时02分
 */
public class LoanUtil {

    /**
     * 根据firstDate往后顺延tenor月
     * @param firstDate
     * @param tenor
     * @return
     */
    public static Date computeDate(Date firstDate, int tenor) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(firstDate);
        gc.add(GregorianCalendar.MONTH, tenor);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return gc.getTime();
    }

    /**
     * 通过反射机制获取某一属性值
     * @param obj
     * @param name
     * @return
     */
    public static Object getValue(Object obj,String name) {
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                if (name.equals(field.getName())) {
                    PropertyDescriptor pd = new PropertyDescriptor(name, clazz);
                    Method method = pd.getReadMethod();
                    return method.invoke(obj);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setValue(Object obj,String name,Object value) {
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                if (name.equals(field.getName())) {
                    PropertyDescriptor pd = new PropertyDescriptor(name, clazz);
                    Method method = pd.getWriteMethod();
                    method.invoke(obj,value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * compute each tenor payment date
     *
     * @param firstDate
     * @param day
     * @return
     */
    public static Date computePerDayDate(Date firstDate, int day) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(firstDate);
        gc.add(GregorianCalendar.DATE, day);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return gc.getTime();
    }

    /**
     * 将整数转换为字符串（若为1位数字，自动在十位补0）
     * @param prefix
     * @param num
     * @return
     */
    public static String int2Str(String prefix,int num) {
        String strNum = "";
        if(num < 10 && num > 0) {
            strNum = prefix + "0" + String.valueOf(num);
        } else {
            strNum = prefix + String.valueOf(num);
        }

        return strNum;
    }


    /**
     * 生成交易流水号
     * @return
     */
    public static String genTransctionSeq() {
        StringBuffer sb = new StringBuffer();
        GregorianCalendar gc = new GregorianCalendar();
        Date date = new Date();
        gc.setTime(date);
        Random r = new Random();
        sb.append("TX").append(gc.getTimeInMillis())
                .append(formatStr(String.valueOf(new BigInteger(165, r).toString(10).toUpperCase().substring(0, 8)),
                        8, true, "0"));
        return sb.toString();
    }

    /**
     * 格式化字符串
     * @param str
     * @param length
     * @param isBefore
     * @param str1
     * @return
     */
    public static String formatStr(String str, int length, boolean isBefore,
                                   String str1) {
        int l = length - str.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < l; i++) {
            sb.append(str1);
        }
        if (isBefore == true) {
            return sb.toString() + str;
        } else {
            return str + sb.toString();
        }
    }
}
